//
//  AppHelper.m
//  Ringminder
//
//  Created by Think Small Studio on 09-6-21.
//  Copyright 2009 Think Small Studio. All rights reserved.
//

#import "AppHelper.h"


@implementation AppHelper
@synthesize dateFormatter;
@synthesize gregorian;
@synthesize weekdaysList;
@synthesize weekdaysAbbreviationList;
@synthesize soundList;
@synthesize alertTimeList;

- (id)init {
    if (self = [super init]) {
        // Initialization code
		dateFormatter = [[NSDateFormatter alloc] init];
		
		gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
		
		weekdaysList = [[NSArray alloc] initWithObjects:@"Every Monday", @"Every Tuesday", @"Every Wednesday",
						  @"Every Thursday", @"Every Friday", @"Every Saturday", @"Every Sunday",  nil];
		
		weekdaysAbbreviationList = [[NSArray alloc] initWithObjects:@"Mon ", @"Tue ", @"Wed ",
						@"Thu ", @"Fri ", @"Sat ", @"Sun ",  nil];
		
		soundList = [[NSArray alloc] initWithObjects:@"None", @"Checkmate", @"Jump",
							   @"Time Passing", @"Time's Up", @"Up Down",  nil];

		alertTimeList = [[NSArray alloc] initWithObjects:@"None", @"5 minutes before", @"15 minutes before",
								   @"30 minutes before", @"1 hour before", @"2 hours before", @"1 day before", @"2 days before", @"On date of event", nil];
    }
    return self;
}

- (NSString *)getTimeString:(NSDate *)time {
	[dateFormatter setDateStyle:NSDateFormatterNoStyle];
	[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
	return [dateFormatter stringFromDate:time];
}

- (NSString *)getRepeatString:(NSMutableArray *)repeat {
	NSMutableString *rtnString = [NSMutableString stringWithCapacity:0];
	NSUInteger i;
	NSNumber *isRepeat;
	for (i = 0; i < [repeat count]; i ++) {
		isRepeat = [repeat objectAtIndex:i];
		
		if ([isRepeat boolValue]) {
			[rtnString appendString:[weekdaysAbbreviationList objectAtIndex:i]];
		}
	}
	
	if ([rtnString compare:@""] == NSOrderedSame) {
		rtnString = @"Never";
	}
	else if ([rtnString compare:@"Mon Tue Wed Thu Fri "] == NSOrderedSame) {
		rtnString = @"Weekdays";
	}
	else if ([rtnString compare:@"Mon Tue Wed Thu Fri Sat Sun "] == NSOrderedSame) {
		rtnString = @"Every day";
	}
	else if ([rtnString compare:@"Sat Sun "] == NSOrderedSame) {
		rtnString = @"Weekends";
	}
	else {
		// do nothing
	}
	
	return rtnString;
}

- (NSString *)getSoundString:(NSUInteger)sound {
	return [soundList objectAtIndex:sound];
}

- (NSString *)getAlertTimeString:(NSUInteger)alerttime {
	return [alertTimeList objectAtIndex:alerttime];
}

- (NSString *)getTodayDateString:(NSDate *)date {
	[dateFormatter setDateStyle:NSDateFormatterFullStyle];
	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	return [dateFormatter stringFromDate:date];
}

- (NSInteger)getTodayDayOfWeek:(NSDate *)date {
	NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:date];

	return [weekdayComponents weekday];
}

- (NSInteger)getHour:(NSDate *)date {
	NSDateComponents *weekdayComponents =[gregorian components:NSHourCalendarUnit fromDate:date];
	
	return [weekdayComponents hour];
}

- (NSInteger)getMinute:(NSDate *)date {	
	NSDateComponents *weekdayComponents =[gregorian components:NSMinuteCalendarUnit fromDate:date];
	
	return [weekdayComponents minute];
}

- (void)dealloc {
	[dateFormatter release];
	[gregorian release];
	[weekdaysList release];
	[weekdaysAbbreviationList release];
	[soundList release];
	[alertTimeList release];
    [super dealloc];
}

@end
